﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Linq;

namespace intelliSys.Management.Automation.Test
{
    /// <summary>
    /// Interaction logic for TestCriteriaMgmt.xaml
    /// </summary>
    public partial class TestCriteriaMgmt : Window
    {
        Storyboard shrink;
        Storyboard expand;
        static ValidationLogic[] validationLogics;
        static TestCriteriaMgmt()
        {
            validationLogics = new ValidationLogic[]
            {
                new ValidationLogic()
                {
                    PreventContinue=true,
                    Summary  = "空域",
                    Detail = "知识点内容不可为空"
                },
                new ValidationLogic()
                {
                    PreventContinue = true,
                    Summary = "空域或错误格式",
                    Detail = "重要性为[0,1]数值"
                },
                new ValidationLogic()
                {
                    PreventContinue = false,
                    Summary = "空域或错误格式",
                    Detail="难度预设为[0,1]数值"
                },
            new ValidationLogic()
            {
                PreventContinue = false,
                Summary = "小心！",
                Detail = "未勾选启用"
            }
            };
            validationLogics[0].ValidateMethod = new ValidationLogic.Validate(ValidationLogic.Non_EmptyLogic);
            validationLogics[2].ValidateMethod = validationLogics[1].ValidateMethod = new ValidationLogic.Validate(ValidationLogic.Double01Range);
            //validationLogics[3].ValidateMethod = new ValidationLogic.Validate(ValidationLogic.UnCheckedNotice);
        }

        public TestCriteriaMgmt()
        {
            this.InitializeComponent();
            CommandType = WindowCommandType._UNSPECIFIED_;
            WindowBehavior = WindowBehaviorCommand.DoNotCreateMore;
            this.Loaded += new RoutedEventHandler(QuestionMgmt_Loaded);
            this.MouseLeftButtonDown += new MouseButtonEventHandler(TestCriteriaMgmt_MouseLeftButtonDown);
            // Insert code required on object creation below this point.
        }

        void TestCriteriaMgmt_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (Cursor == Cursors.Help)
            {
                if (e.OriginalSource is Run)
                    Message.Show(this, "Help System", MessageType.Comfirm, "[CDATA] IS NULL", "关于" + (e.OriginalSource as Run).Text + "的帮助");
                else if (e.OriginalSource is TextBlock)
                    Message.Show(this, "Help System", MessageType.Comfirm, "[CDATA] IS NULL", "关于" + (e.OriginalSource as TextBlock).Text + "的帮助");
                else
                    Message.Show(this, "Help System", MessageType.Comfirm, "您的鼠标未落在帮助点上。通常，帮助点是显示在程序中的文字，它们不包含在以下内容中\n1)文本框\n2)按钮", "无法找到类型为" + e.OriginalSource.GetType().Name + "的帮助");
                return;
            }
            this.DragMove();
        }
        public WindowCommandType CommandType
        {
            get;
            set;
        }
        public WindowCommandResult DialogueResult
        {
            get
            {
                return dialogueResult;
            }
        }
        public WindowBehaviorCommand WindowBehavior
        {
            get;
            set;
        }
        WindowCommandResult dialogueResult = WindowCommandResult._UNSPECIFIED_;
        void QuestionMgmt_Loaded(object sender, RoutedEventArgs e)
        {
            shrink = Resources["Shrink"] as Storyboard;
            expand = Resources["Expand"] as Storyboard;
            validationLogics[0].FrameworkElement = BdyText;
            validationLogics[1].FrameworkElement = BdyText3;
            validationLogics[2].FrameworkElement = BdyText1;
            validationLogics[3].FrameworkElement = EnableKnowledge;
        }
        Grid lastFocus;
        private void Grid_GotFocus(object sender, RoutedEventArgs e)
        {
            if ((sender as Grid) == lastFocus)
            {
                lastFocus = sender as Grid;
                return;
            }
            else
                lastFocus = sender as Grid;
            (expand.Children[0] as DoubleAnimationUsingKeyFrames).KeyFrames[1].Value = groupBox1.ActualHeight - 300;
            expand.Begin(sender as FrameworkElement);
        }

        private void Grid_LostFocus(object sender, RoutedEventArgs e)
        {
            if ((sender as Grid) != lastFocus)
            {
                lastFocus = sender as Grid;
                return;
            }
            else
                lastFocus = null;
            (shrink.Children[0] as DoubleAnimationUsingKeyFrames).KeyFrames[0].Value = groupBox1.ActualHeight - 300;
            shrink.Begin(sender as FrameworkElement);
        }

        private void OnSiteHelp_Click(object sender, RoutedEventArgs e)
        {
            var HelpEnabled = Cursor == Cursors.Help;
            if (HelpEnabled)
            {
                HelpIndicator.Text = "已关闭OnSite帮助";
                Cursor = Cursors.Arrow;
            }
            else
            {
                HelpIndicator.Text = "已开启OnSite帮助";
                Cursor = Cursors.Help;
            }
        }

        private void CheckList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var item = CheckList.SelectedItem;
            if (item == null)
                return;
            var current = (item as ValidationLogic).FrameworkElement;
            while (current.Parent.GetType() != typeof(TabItem))
            {
                current = current.Parent as FrameworkElement;
            }
            if (current != null)
            {
                (current.Parent as TabItem).IsSelected = true;
            }
            (item as ValidationLogic).FrameworkElement.Focus();
        }

        private void Comfirm_Click(object sender, RoutedEventArgs e)
        {
            CheckList.Items.Clear();
            int warning = 0;
            foreach (var validate in validationLogics)
            {
                if (!validate.ValidateMethod(validate.FrameworkElement))
                {
                    if (!validate.PreventContinue)
                    {
                        warning++;
                    }
                    CheckList.Items.Add(validate);
                }
            }
            if (CheckList.Items.Count - warning != 0)
            {
                Message.Show(this, "验证错误", MessageType.Comfirm, "此页存在一个或多个验证错误，在继续之前，您必须更正它们。\n通常，验证框出现在上一屏幕的左下角。", "验证错误阻止继续进行");
                return;
            }
            if (warning != 0)
            {
                var result = Message.Show(this, "验证错误", MessageType.Comfirm | MessageType.Cancel, "此页存在一个或多个验证警告，在继续之前，建议您更正它们。\n通常，验证框出现在上一屏幕的左下角。\n单击确定以继续，取消以返回", "验证警告可能导致意外后果");
                if (result != MessageType.Comfirm)
                {
                    return;
                }
            }
            if (CommandType == WindowCommandType.CreateNew)
            {
                dialogueResult = WindowCommandResult.CreateNewSavingChanges;
            }
            else
            {
                dialogueResult = WindowCommandResult.OpenSavingChanges;
            }
            WindowBehavior = WindowBehaviorCommand.DoNotCreateMore;
            this.Close();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            if (CommandType == WindowCommandType.Open)
            {
                dialogueResult = WindowCommandResult.OpenDiscardingChanges;
            }
            else
            {
                dialogueResult = WindowCommandResult.CreateNewDiscardingChanges;
            }
            this.Close();
        }

        private void ComfirmAndContinue_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Comfirm_Click(null, null);
            WindowBehavior = WindowBehaviorCommand.CreateMore;
            // TODO: Add event handler implementation here.
        }


        private void BdyText3_MouseEnter(object sender, MouseEventArgs e)
        {
            (sender as TextBox).Focus();
            (sender as TextBox).SelectAll();
        }



    }
}